# -*- coding: utf-8 -*-
import os
import uuid
import zipfile
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from time import sleep


class WrodToJpg:
    def __init__(self, workdir):
        self.workdir = workdir;
        self.wordtojpgurl = 'http://www.docpe.com/word/word-to-image.aspx'

    def docsToPics(self):
        files = os.listdir(self.workdir)
        for f in files:
            # f的名称即为试卷id
            temp = os.path.join(self.workdir, f);
            if (not os.path.isdir(temp)):
                continue;
            fpath = os.path.join(self.workdir, f)
            self.foreachDoc(fpath, f)

    def getUUid(self):
        return str(uuid.uuid1())

    def foreachDoc(self, fpath, paperid):
        files = os.listdir(fpath)
        for f in files:
            docpath = os.path.join(fpath, f);
            if (f.endswith('doc')):
                self.docToJpg(fpath, docpath)
            elif (f.endswith('pdf')):
                self.pdfToJpg(fpath, docpath)
            else:
                continue;
            sleep(1000)
            self.unzipPics(fpath)

    def unzip(self, path, extrapath):
        f = zipfile.ZipFile(path, 'r')
        for file in f.namelist():
            f.extract(file, extrapath);

    def unzipPics(self, path):
        files = os.listdir(path)
        for f in files:
            if (f.endswith('zip')):
                zippath = os.path.join(path, f)
                self.unzip(zippath, path)
        files=os.listdir(path)
        for f in files:
            if(f.endswith('png')):
                tf=f[f.index('-'):]
                os.rename(f,tf)
    def docToJpg(self, topath, fpath):
        driver = self.getDriver(topath)
        driver.get('http://wordtojpeg.com/zh/');
        select = driver.find_element_by_id('upload-buttons-wrapper').find_element_by_tag_name('input')
        select.send_keys(fpath);
        WebDriverWait(driver, 200).until(EC.visibility_of(driver.find_element_by_xpath(
            "//button[contains(@id,'download-all') and not(contains(@class,'disabled'))]")))
        btn = driver.find_element_by_id('download-all')
        btn.click()
        driver.quit();

    def pdfToJpg(self, topath, fpath):
        driver = self.getDriver(topath)
        driver.get('http://pdftoimage.com/zh/');
        select = driver.find_element_by_id('upload-buttons-wrapper').find_element_by_tag_name('input')
        select.send_keys(fpath);
        WebDriverWait(driver, 200).until(EC.visibility_of(driver.find_element_by_xpath(
            "//button[contains(@id,'download-all') and not(contains(@class,'disabled'))]")))
        btn = driver.find_element_by_id('download-all')
        btn.click()
        driver.quit();

    def getDriver(self, topath):
        options = webdriver.ChromeOptions()
        prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': topath}
        options.add_experimental_option('prefs', prefs)
        driver = webdriver.Chrome(chrome_options=options);
        return driver


workdir = '/work/temp/xuekewang'
wt = WrodToJpg(workdir)
wt.docsToPics();
